import json
from google.appengine.ext import ndb
from model_PC import PC

class Parent (ndb.Model):
    """Models an individual Parent entry with content and id (email address)"""
    first_name =     ndb.StringProperty()
    last_name =      ndb.StringProperty()
    phone_number =   ndb.StringProperty()
    email_address =  ndb.StringProperty()

    @staticmethod
    def create_parent_from_json(parent_info_json):
        """ Create and put a new parent in db, make sure parent id is not exists """
        data = json.loads(parent_info_json)
        parent = Parent(first_name =        data.get("first_name"),
                        last_name =         data.get("last_name"),
                        phone_number =      data.get("phone_number"),
                        email_address =     data.get("email_address"))

        check_parent_exists = Parent.query(Parent.email_address == data.get("email_address")).get()
        if check_parent_exists is not None:
            return 1
        return parent.put()

    @staticmethod
    def delete_parent_by_id (given_email_address):
        """ Delete a child from db, by child id, if child id doesnt exists return -1 """
        check_parent_exists = Parent.query(Parent.email_address == given_email_address).get()
        if check_parent_exists is None:
            return -1
        else:
            check_parent_exists.key.delete()
            PC.delete_entities_by_parent_id(given_email_address)
            return 1

    @staticmethod
    def get_parent_info(given_parent_id):
        """ returns info about a parent with a given parent id """
        check_parent_exists = Parent.query(Parent.email_address == given_parent_id).get()
        if check_parent_exists is None:
            return -1
        else:
            data = {"first_name" : check_parent_exists.first_name,
                    "last_name" : check_parent_exists.last_name,
                    "phone_number":    check_parent_exists.phone_number,
                    "email_address": check_parent_exists.email_address}
            data_json = json.dumps(data)
            return data_json